package com.wfx.task;


import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.wfx.service.IWxbLoginLogService;
import com.wfx.service.IWxbOrderCountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import com.wfx.service.IWxbOrderService;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

@Component
public class Jop {
    @Autowired
    private IWxbOrderCountService iWxbOrderCountService;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Autowired
    private ObjectMapper objectMapper;
    @Autowired
    private IWxbLoginLogService iWxbLoginLogService;

    /**
     * 每天的凌晨一点整
     *
     * 根据订单表统计每天的订单数
     */

    //@Scheduled(cron = "0 0 1 * * ?")
    @Scheduled(cron = "9 48 8 * * *?")

    //每小时的 5，20
    public  void  orderCount(){

        //统计的结果
        List<Map> maps = iWxbOrderCountService.statOrderCuntPerMouth();


        //存放到Redis中(hash)
        String key = "stat:orderCount";
        String field = "countPerMouth";
        try {
            String value = objectMapper.writeValueAsString(maps);
            stringRedisTemplate.boundHashOps(key).put(field,value);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }



    }






    @Autowired
    private IWxbOrderService iWxbOrderService;

    //每月订单数量

    @Scheduled(cron = "6 48 8 * * ?")

    public void monthJob() {
        List<Map>maps =iWxbOrderService.statOrderCuntPerMoth();
        String key = "stat:order";
        String field = "cuntPerMoth";
        try {
            String value = objectMapper.writeValueAsString(maps);
            stringRedisTemplate.boundHashOps(key).put(field,value);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    //统计每天的登录人数

    @Scheduled(cron = "3 48 8 * * ?")

    public void loginCountPerson(){
        List<Map> maps = iWxbLoginLogService.statLoginCountPersonPerDay();
        String key="stat:loginPerson";
        String field="cuntLoginPersonPerDay";
        try {
            String value = objectMapper.writeValueAsString(maps);
            stringRedisTemplate.boundHashOps(key).put(field,value);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
    }
    //统计每天的登录人次


    @Scheduled(cron = "1 48 8 * * ?")

    public void loginCount(){
        List<Map> maps = iWxbLoginLogService.statLoginCountPerDay();
        String key ="stat:login";
        String field ="cuntLoginPerDay";
        try {
            String value = objectMapper.writeValueAsString(maps);
            stringRedisTemplate.boundHashOps(key).put(field,value);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
    }



    //李轩
    @Scheduled(cron = "8 48 8 * * ?")
    public  void   statOrdercuntPerDay(){
        //统计的结果
        List<Map> maps = iWxbOrderService.statOrdercuntPerDay();
        //存放到Redis中(hash)
        String key = "stat:countOrder";
        String field = "countPerDay";
        try {
            String value = objectMapper.writeValueAsString(maps);
            stringRedisTemplate.boundHashOps(key).put(field,value);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
    }


    @Scheduled(cron = "2 47 8 * * ?")
    public  void   statOderCuntPerMoney(){
        //统计的结果
        List<Map> maps = iWxbOrderService.statOrdercuntPerDay();
        //存放到Redis中(hash)
        String key = "stat:countOrderMoney";
        String field = "countPerMoney";
        try {
            String value = objectMapper.writeValueAsString(maps);
            stringRedisTemplate.boundHashOps(key).put(field,value);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
    }





}
